xl: Implement block-detach command
authorKeir Fraser <keir.fraser@citrix.com>
Thu, 13 May 2010 07:52:47 +0000 (08:52 +0100)
committerKeir Fraser <keir.fraser@citrix.com>
Thu, 13 May 2010 07:52:47 +0000 (08:52 +0100)
Signed-off-by: Eric Chanudet <eric.chanudet@citrix.com>
tools/libxl/libxl_utils.c
tools/libxl/libxl_utils.h
tools/libxl/xl_cmdimpl.c
tools/libxl/xl_cmdimpl.h
tools/libxl/xl_cmdtable.c

index 71b5766277b8c759b7d698d8131f2182f05cb3f4..620c254dafbb5f78ab010b0ecadd0a1a6071d8f5 100644 (file)
@@ -430,3 +430,38 @@ int libxl_devid_to_device_nic(struct libxl_ctx *ctx, uint32_t domid,
     libxl_free(ctx, nic_path_be);
     return 0;
 }
+
+int libxl_devid_to_device_disk(struct libxl_ctx *ctx, uint32_t domid,
+                               const char *devid, libxl_device_disk *disk)
+{
+    char *endptr, *val;
+    char *dompath, *diskpath, *be_path;
+    unsigned int devid_n;
+
+    devid_n = strtoul(devid, &endptr, 10);
+    if (devid == endptr) {
+        return ERROR_INVAL;
+    }
+    dompath = libxl_xs_get_dompath(ctx, domid);
+    diskpath = libxl_sprintf(ctx, "%s/device/vbd/%s", dompath, devid);
+    if (!diskpath) {
+        return ERROR_FAIL;
+    }
+
+    val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/backend-id", diskpath));
+    disk->backend_domid = strtoul(val, NULL, 10);
+    disk->domid = domid;
+    be_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/backend", diskpath));
+    disk->physpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/params", be_path));
+    val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/type", be_path));
+    libxl_string_to_phystype(ctx, val, &(disk->phystype));
+    disk->virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev", be_path));
+    val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/removable", be_path));
+    disk->unpluggable = !strcmp(val, "1");
+    val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/mode", be_path));
+    disk->readwrite = !!strcmp(val, "w");
+    val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/device-type", diskpath));
+    disk->is_cdrom = !strcmp(val, "cdrom");
+
+    return 0;
+}
index 06df2b84a856f13f23e69f155622c4b83c7a4ea9..bd3debf0b0f44bef318c7036dc406d874904548f 100644 (file)
@@ -61,6 +61,9 @@ int libxl_mac_to_device_nic(struct libxl_ctx *ctx, uint32_t domid,
 int libxl_devid_to_device_nic(struct libxl_ctx *ctx, uint32_t domid,
                               const char *devid, libxl_device_nic *nic);
 
+int libxl_devid_to_device_disk(struct libxl_ctx *ctx, uint32_t domid,
+                               const char *devid, libxl_device_disk *disk);
+
 /* log levels: */
 #define XL_LOG_DEBUG 3
 #define XL_LOG_INFO 2
index 41b3db834c465d1b04c53d473d8776085cba0a39..4efd114aedbe595f65ac86a6d0a7f9e4b82ba673 100644 (file)
@@ -3461,3 +3461,37 @@ int main_blocklist(int argc, char **argv)
     }
     exit(0);
 }
+
+int main_blockdetach(int argc, char **argv)
+{
+    int opt;
+    libxl_device_disk disk;
+
+    if (argc != 3) {
+        help("block-detach");
+        exit(0);
+    }
+    while ((opt = getopt(argc, argv, "h")) != -1) {
+        switch (opt) {
+        case 'h':
+            help("block-detach");
+            exit(0);
+        default:
+            fprintf(stderr, "option `%c' not supported.\n", opt);
+            break;
+        }
+    }
+
+    if (domain_qualifier_to_domid(argv[1], &domid, 0) < 0) {
+        fprintf(stderr, "%s is an invalid domain identifier\n", argv[1]);
+        exit(1);
+    }
+    if (libxl_devid_to_device_disk(&ctx, domid, argv[2], &disk)) {
+        fprintf(stderr, "Error: Device %s not connected.\n", argv[2]);
+        exit(1);
+    }
+    if (libxl_device_disk_del(&ctx, &disk, 1)) {
+        fprintf(stderr, "libxl_device_del failed.\n");
+    }
+    exit(0);
+}
index 5701b4c7a01021c8d9cbeb78f10b4a90ee7336c0..4d8d4be9157c53f966fd4e4dc1eee4b3efe24453 100644 (file)
@@ -47,5 +47,6 @@ int main_networklist(int argc, char **argv);
 int main_networkdetach(int argc, char **argv);
 int main_blockattach(int argc, char **argv);
 int main_blocklist(int argc, char **argv);
+int main_blockdetach(int argc, char **argv);
 
 void help(char *command);
index 1539c2047fb78adbf19b17b2004f6276ed7d6a6d..bbf11677f6d1f8bbe622c6647230f4a5fc7dd81c 100644 (file)
@@ -214,6 +214,11 @@ struct cmd_spec cmd_table[] = {
       "List virtual block devices for a domain",
       "<Domain(s)>",
     },
+    { "block-detach",
+      &main_blockdetach,
+      "Destroy a domain's virtual block device",
+      "<Domain> <DevId>",
+    },
 };
 
 int cmdtable_len = sizeof(cmd_table)/sizeof(struct cmd_spec);